package com.meitianhui.convenitenbuy.module.wallet.adapter;

import android.content.Context;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.wallet.model.RedPacketBean;
import com.meitianhui.convenitenbuy.widget.Spanny;

import java.util.List;

/**
 * Created by Administrator on 2017/11/3.
 */
public class WalletPacketsAdapter extends BaseMultiItemQuickAdapter<RedPacketBean, BaseViewHolder> {
    private Context context;

    private OnPacketItemClickListener mListener;
    private List<RedPacketBean> mData;

    public WalletPacketsAdapter(Context context, List<RedPacketBean> data) {
        super(data);
        this.context = context;
        addItemType(RedPacketBean.ITEM_ENABLE, R.layout.item_wallet_packets_enable);
        addItemType(RedPacketBean.ITEM_DISABLE, R.layout.item_wallet_packets_disable);
        mData = data;

    }

    @Override
    protected void convert(BaseViewHolder helper, RedPacketBean item) {
        int itemViewType = helper.getItemViewType();
        switch (itemViewType) {
            case RedPacketBean.ITEM_ENABLE:
                String gift_type = item.getGift_type();
                if (gift_type == null) {
                    helper.setImageResource(R.id.iv_wallet_packets_item_enable, R.mipmap.wallet_packets_item_gold);
                    helper.setBackgroundRes(R.id.rl_wallet_packets_item_enable, R.mipmap.wallet_packets_item_bg_gold);
                    helper.setTextColor(R.id.tv_wallet_packets_item_enable_number, context.getResources().getColor(R.color.wallet_packets_gold_color));
                    helper.setVisible(R.id.tv_wallet_packets_item_gift, false);
                    return;
                }
                helper.setText(R.id.tv_wallet_packets_item_enable_date, item.getCreated_date());
                TextView view = helper.getView(R.id.tv_wallet_packets_item_enable_number);
                if (gift_type.equals("gold")) {
                    helper.setImageResource(R.id.iv_wallet_packets_item_enable, R.mipmap.wallet_packets_item_gold);
                    helper.setBackgroundRes(R.id.rl_wallet_packets_item_enable, R.mipmap.wallet_packets_item_bg_gold);
                    helper.setTextColor(R.id.tv_wallet_packets_item_enable_number, context.getResources().getColor(R.color.wallet_packets_gold_color));
                    helper.setVisible(R.id.tv_wallet_packets_item_gift, false);
                    view.setText(item.getGift_value());
                } else if (gift_type.equals("cash")) {
                    helper.setImageResource(R.id.iv_wallet_packets_item_enable, R.mipmap.wallet_packets_item_change);
                    helper.setBackgroundRes(R.id.rl_wallet_packets_item_enable, R.mipmap.wallet_packets_item_bg_change);
                    helper.setTextColor(R.id.tv_wallet_packets_item_enable_number, context.getResources().getColor(R.color.wallet_packets_cash_color));
                    helper.setVisible(R.id.tv_wallet_packets_item_gift, false);
                    convertChange(view, item.getGift_value());
                } else if (gift_type.equals("face_gift")) {
                    helper.setImageResource(R.id.iv_wallet_packets_item_enable, R.mipmap.wallet_packets_item_change);
                    helper.setBackgroundRes(R.id.rl_wallet_packets_item_enable, R.mipmap.wallet_packets_item_bg_change);
                    helper.setTextColor(R.id.tv_wallet_packets_item_enable_number, context.getResources().getColor(R.color.wallet_packets_cash_color));
                    helper.setVisible(R.id.tv_wallet_packets_item_gift, true);
                    convertChange(view, item.getGift_value());
                }

                RelativeLayout layoutEnable = helper.getView(R.id.rl_wallet_packets_item_enable);
                layoutEnable.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onEnablePacketItemClick(helper.getAdapterPosition(), item);
                    }
                });
                break;
            case RedPacketBean.ITEM_DISABLE:
                RelativeLayout layout = helper.getView(R.id.rl_wallet_packets_item_disable);
                layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onDisablePacketItemClick(helper.getAdapterPosition(), item);
                    }
                });
                break;
        }
    }

    private void convertChange(TextView view, String looseChange) {
        if (looseChange == null)
            return;
        String number;
        String decimal;
        if (looseChange.contains(".")) {
            int i = looseChange.indexOf(".");
            number = looseChange.substring(0, i);
            decimal = looseChange.substring(i);
        } else {
            number = looseChange;
            decimal = ".00";
        }
        Spanny spanny = new Spanny(number, new TextAppearanceSpan(
                context, R.style.red_packet_big_text_balance))
                .append(decimal, new TextAppearanceSpan(context, R.style.red_packet_small_text_balance));
        view.setText(spanny);
        view.setTextSize(50);
    }

    public interface OnPacketItemClickListener {
        void onEnablePacketItemClick(int position, RedPacketBean bean);
        void onDisablePacketItemClick(int position, RedPacketBean bean);
    }

    public void setOnPacketsItemClickListener(OnPacketItemClickListener listener) {
        this.mListener = listener;
    }


}
